package com.yan.kfgl.service.dao.spring;

import ins.yan.framework.model.Page;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yan.kfgl.model.Jtext;
import com.yan.kfgl.service.dao.facade.JtextDaoService;

public class JtextDaoServiceSpringImpl extends HibernateDaoSupport implements JtextDaoService{
	
	public int findJtextMaxId() {
		String hql = "select max(id) From com.yan.kfgl.model.Jtext";
		int maxId;
		try {
			maxId = (Integer)super.getHibernateTemplate().find(hql).get(0);
		} catch (NullPointerException e) {
			maxId = 0;
		}
		return maxId;
	}

	public void saveJtext(Jtext jtext) {
		super.getHibernateTemplate().save(jtext);
	}

	public List<Jtext> findAllJtext() {
		
		return super.getHibernateTemplate().loadAll(Jtext.class);
	}

	public Long findJtextListTotalPage(Page page , String info) {
		String hql = "select count(t.id) From Jtext t ";
		Object[] objects;
		if(info != null && !"".equals(info.trim())){
			hql += "where t.name like ? or t.content like ?";
			objects = new Object[]{ "%"+info+"%" , "%"+info+"%" };
		}else{
			objects = new Object[]{};
		}
		List list = super.getHibernateTemplate().find(hql, objects);
		if(list == null || list.size()==0){
			return 0L;
		}else{
			Long size = (Long)list.get(0);
			if(size%page.getSize() == 0){
				return size/page.getSize();
			}else{
				return size/page.getSize()+1;
			}
		}
	}
	
	public List<Jtext> findJtextList(Page page , String info) {
		String hql = " From Jtext t ";
		Object[] objects;
		if(info != null && !"".equals(info.trim())){
			hql += "where t.name like ? or t.content like ?";
			objects = new Object[]{ "%"+info+"%" , "%"+info+"%" };
		}else{
			objects = new Object[]{};
		}
		hql += " order by t.id";
		int size = Page.DEFAULT_INDEX;
		int index = Page.DEFAULT_INDEX;
		if(page!=null && page.getSize()!= 0){
			size = page.getSize();
		}
		if(page!=null && page.getIndex()!= 0){
			index = page.getIndex();
		}
		
		return this.queryListObjectAllForPage(hql, objects , size, index);
	}

	public List queryListObjectAllForPage(String queryString,Object[] objects , int pageSize,int page){
		Session session = super.getHibernateTemplate().getSessionFactory().openSession();
		Query   query   =   session.createQuery(queryString);
		for(int i=0 ; i<objects.length ; i++){
			query.setParameter(i, objects[i]);
		}
		query.setFirstResult((page-1)*pageSize); 
		query.setMaxResults(pageSize); 
		List list= (List)query.list();
		session.close();
		return list;
	}
	public List<Jtext> findJtextListByIds(Integer[] ids) {
		if(ids == null || ids.length == 0){
			return new ArrayList<Jtext>(0);
		}
		String hql = "from Jtext where id in (";
		for(int i=0;i<ids.length;i++){
			hql += "?";
			if(i != (ids.length-1)){
				hql += ",";
			}
		}
		hql += ")";
		return super.getHibernateTemplate().find(hql, ids);
	}
	
	public List<Jtext> findJtextEqualCondition(String condition, Object value) {
		String hql = "from Jtext t where t." + condition + "=?";
		return super.getHibernateTemplate().find(hql, value);
	}

	public List<Jtext> findJtextLikeCondition(String condition, Object value) {
		String hql = "from Jtext t where t." + condition + " like ?";
		return super.getHibernateTemplate().find(hql, value);
	}

	public void deleteJtextList(List<Jtext> jtexts) {
		super.getHibernateTemplate().deleteAll(jtexts);
	}

	public Jtext findtexteByPK(Jtext jtext) {
		
		return (Jtext) super.getHibernateTemplate().get(Jtext.class, jtext.getId());
	}

	public void updateJtext(Jtext jtext) {
		super.getHibernateTemplate().update(jtext);
		
	}

	public void deleteJtextByPk(Jtext jtext) {
		super.getHibernateTemplate().delete(jtext);
		
	}

}